//
//  ModifyPasswordViewController.m
//  HZHome
//
//  Created by SunShine on 13-10-16.
//  Copyright (c) 2013年 xzysun. All rights reserved.
//

#import "ModifyPasswordViewController.h"
#define INPUT_VIEW_OFFSET 100.0

@interface ModifyPasswordViewController ()
-(void)showAlert:(NSString *)msg;
@end

@implementation ModifyPasswordViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"密码修改";
    self.oldPassField.inputAccessoryView = [self getInputAccessoryView];
    self.theNewPassField.inputAccessoryView = [self getInputAccessoryView];
    self.confirmPassField.inputAccessoryView = [self getInputAccessoryView];
    self.confirmPassField.delegate = self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)commitButtonAction:(id)sender
{
    NSString *oldPass = self.oldPassField.text;
    if (!oldPass || oldPass.length == 0) {
        [self showAlert:@"原密码不能为空."];
        return;
    }
    if (![md5HexDigest(oldPass) isEqualToString:[UserInfoService getUserInfoService].lastLoginPassword]) {
        [self showAlert:@"原密码输入不正确."];
        return;
    }
    NSString *newPass = self.theNewPassField.text;
    if (!newPass || newPass.length == 0) {
        [self showAlert:@"新密码不能为空."];
        return;
    }
    NSString *confirmPass = self.confirmPassField.text;
    if (!confirmPass || confirmPass.length == 0) {
        [self showAlert:@"确认密码不能为空."];
        return;
    }
    if (![newPass isEqualToString:confirmPass]) {
        [self showAlert:@"新密码与确认密码不相同."];
        return;
    }
    [UserInfoService getUserInfoService].delegate = self;
    [[UserInfoService getUserInfoService]modifyUserPassword:newPass];
    [self startLoadingViewWithText:@"正在保存"];
}

-(void)showAlert:(NSString *)msg
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:msg delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}

#pragma mark - User Service Delegate
-(void)loadUserInfoFinished:(id)result
{
    [self stopLoadingView];
    [self showAlert:@"修改成功"];
    //更新自动登录保存的密码
    NSString *userPass = [DataStoreService loadFromKeychain:AUTO_LOGIN_USER_PASSWORD];
    if (userPass) {
        [DataStoreService saveToKeychain:self.theNewPassField.text data:AUTO_LOGIN_USER_PASSWORD];
    }
}

-(void)loadUserInfoFaild:(NSString *)msg
{
    [self stopLoadingView];
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"错误" message:msg delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}

#pragma mark - Text Field Delegate
//开始输入的时候向上移动整个界面，这个输入框不会被挡住
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    [UIView beginAnimations:@"animation" context:nil];
    [UIView setAnimationDuration:0.3f];
    CGRect frame = self.view.frame;
    frame.origin.y = frame.origin.y - INPUT_VIEW_OFFSET;
    self.view.frame = frame;
    [UIView commitAnimations];
}

//输入结束的时候把整个界面向下移动，这个能恢复正常
-(void)textFieldDidEndEditing:(UITextField *)textField
{
    [UIView beginAnimations:@"animation" context:nil];
    [UIView setAnimationDuration:0.3f];
    CGRect frame = self.view.frame;
    frame.origin.y = frame.origin.y + INPUT_VIEW_OFFSET;
    self.view.frame = frame;
    [UIView commitAnimations];
}
@end
